<template>
  <div class="">
    <h1>{{id ? '编辑' : '新建'}}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs value="skills">
        <!-- 基础信息 -->
        <el-tab-pane label="基础信息" name="base">
          <el-form-item label="名称">
            <el-input v-model="model.name" clearable></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title" clearable></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:3000/admin/api/upload"
              :headers="getAuthHeadersByMixin()"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
              <img v-if="model.avatar" :src="model.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="model.categories" placeholder="请选择" clearable multiple>
              <el-option
                v-for="item in categoryOptions"
                :key="item._id"
                :label="item.name"
                :value="item._id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate 
              style="margin-top: 0.8em;"
              :max="9" 
              show-score 
              v-model="model.scores.difficult" 
              clearable></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate 
              style="margin-top: 0.8em;"
              :max="9" 
              show-score 
              v-model="model.scores.skills" 
              clearable></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate 
              style="margin-top: 0.8em;"
              :max="9" 
              show-score 
              v-model="model.scores.attack" 
              clearable></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate 
              style="margin-top: 0.8em;"
              :max="9" 
              show-score 
              v-model="model.scores.survive" 
              clearable></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.downWind" placeholder="请选择" clearable multiple>
              <el-option
                v-for="item in equipmentOptions"
                :key="item._id"
                :label="item.name"
                :value="item._id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.upWind" placeholder="请选择" clearable multiple>
              <el-option
                v-for="item in equipmentOptions"
                :key="item._id"
                :label="item.name"
                :value="item._id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips" clearable></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips" clearable></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips" clearable></el-input>
          </el-form-item>
        </el-tab-pane>
        <!-- 技能 -->
        <el-tab-pane label="技能" name="skills">
          <el-button size="small" @click="model.skills.push({})"><i class="el-icon-plus"></i>添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap;">
            <el-col :md="12" v-for="(item, index) in model.skills" :key="index">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  action="http://localhost:3000/admin/api/upload"
                  :headers="getAuthHeadersByMixin()"
                  :show-file-list="false"
                  :on-success="res => $set(item, 'icon', res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item style="margin-top:10px;">
        <el-button type="primary" native-type="submit">{{id ? '保存' : '新建'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
  
</template>

<script>
import { putHeroCreate, getHeroEditDetail, putHeroEdit } from '@/api/hero'
import { getCategoryList } from '@/api/category'
import { getItemList } from '@/api/item'
export default {
  name: "HeroEdit",
  props: {
    id: {
      type: String
    }
  },
  data() {
    return {
      model: {
        skills: [],
        scores: {
          difficult: 0,
          skills: 0,
          attack: 0,
          survive: 0,
        }
      },
      categoryOptions: [],
      equipmentOptions: []
    };
  },
  created() {
    this.getCategoryList()
    this.getequipmentList()
    this.id && this.getHeroEditDetail()
  },
  methods: {
    // 文件上传
    handleAvatarSuccess(file) {
      this.$set(this.model, 'avatar', file.url) // 显示赋值
      // this.model.avatar = file.url
    },
    //获取分类列表
    async getCategoryList() {
      let params = {
        type: 'hero'
      }
      let res = await getCategoryList(params)
      this.categoryOptions = res.data.data
    },
    //获取装备列表
    async getequipmentList() {
      let res = await getItemList()
      this.equipmentOptions = res.data.data
    },
    async save() {
      let data = this.model
      if (this.id) {
        await putHeroEdit(this.id,data) //编辑数据
      } else {
        await putHeroCreate(data) //创建数据
      }
      this.$router.push('/heroes/list')
      this.$message({
        type: 'success',
        message: this.id ? '编辑成功' : '创建成功'
      })
    },
    // 获取编辑数据
    async getHeroEditDetail() {
      let res = await getHeroEditDetail(this.id)
      this.model = Object.assign({}, this.model, res.data)
    },
  }
};
</script>

<style lang="less" scoped></style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
